﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Linq;
using SP_XHWeb.WebParts.GetShowList;

namespace SP_XHWeb.WebParts.Phone_GetNewsList
{
    public partial class Phone_GetNewsListUserControl : UserControl
    {
        #region 自定义属性
        public string ListURL;
        public string ListName;
        public string ListNum;
        #endregion

        public string NewsDetailsURL = "/_layouts/SP_XHWeb/NewsPhoneDetails.aspx?1=1";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(ListURL) && !string.IsNullOrEmpty(ListName))
                {
                    gvEphone.DataSource = GetPhoneNewsList();
                    gvEphone.DataBind();
                    
                }
            }
        }

        /// <summary>
        /// 防止GridView翻页时报错
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvEphone_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEphone.PageIndex = e.NewPageIndex;
            gvEphone.DataSource = GetPhoneNewsList();
            gvEphone.DataBind();

        }

        /// <summary>
        /// 根据WebPart自定义属性获取News
        /// </summary>
        /// <returns></returns>
        protected List<ShowDetails> GetPhoneNewsList()
        {
            List<ShowDetails> lists = new List<ShowDetails>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(ListURL) && !string.IsNullOrEmpty(ListName))
            {
                using (SPSite site = new SPSite(hostUrl))
                {
                    using (SPWeb web = site.OpenWeb(ListURL))
                    {
                        SPList list = web.Lists.TryGetList(ListName);
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                string newslink = hostUrl + NewsDetailsURL + "&web_url=" + ListURL + "&list_title=" + ListName + "&item_id=" + item.ID;
                                ShowDetails sd = new ShowDetails();
                                sd.SID = item.ID;//新闻ID
                                sd.Title = item.Title;//新闻标题
                                sd.Content = newslink;//链接
                                sd.Categories = item["创建时间"].ToString();
                                //lists.Add(new PNewsListItems(item.ID, item.Title, DateTime.Parse(item["创建时间"].ToString()), newslink));
                                lists.Add(sd);
                            }
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(ListNum))
            {
                if (lists != null && lists.Count != 0)
                {
                    try
                    {
                        int num = Int32.Parse(ListNum);
                        lists = (from temp in lists orderby temp.SID descending select temp).Take(num).ToList<ShowDetails>();
                    }
                    catch
                    {
                        lists = (from temp in lists orderby temp.SID descending select temp).ToList<ShowDetails>();
                    }
                }
            }
            else
            {
                lists = (from temp in lists orderby temp.SID descending select temp).ToList<ShowDetails>();
            }
            return lists;
        }
    }

    //public class PNewsListItems
    //{
    //    public int PNewsID;
    //    public string PNewsTitle;
    //    public DateTime PNewsCreateDate;
    //    public string PNewsUrl;
    //    public PNewsListItems(int newsID, string newsTitle, DateTime newsCreateDate, string newsUrl)
    //    {
    //        this.PNewsID = newsID;
    //        this.PNewsTitle = newsTitle;
    //        this.PNewsCreateDate = newsCreateDate;
    //        this.PNewsUrl = newsUrl;
    //    }
    //}
}
